package com.example.adapter;

import java.util.List;

import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.TextView;

import com.example.dao.Diary;
import com.example.notering.R;

public class DiaryAdapter extends BaseAdapter{

	protected LayoutInflater mInflater;
    protected List<Diary> itemList;

	public DiaryAdapter(Context context, List<Diary> list) {
		this.mInflater = LayoutInflater.from(context);
        this.itemList = list;
	}
	
	@Override
	public int getCount() {
		return itemList.size();
	}

	@Override
	public Object getItem(int position) {
		return itemList.get(position);
	}

	@Override
	public long getItemId(int position) {
		// TODO Auto-generated method stub
		return 0;
	}

	@Override
    public View getView(int position, View convertView, ViewGroup parent) {
		ViewHolder holder = null;

        if (convertView == null) {

            holder = new ViewHolder();
            convertView = mInflater.inflate(R.layout.list_item_diary, null);
            //初始化
            holder.cover_diary = (ImageView)convertView.findViewById(R.id.list_cover_diary);
            holder.title_diary = (TextView)convertView.findViewById(R.id.list_title_diary);
            holder.content_diary = (TextView)convertView.findViewById(R.id.list_content_diary);
            //标记
            convertView.setTag(holder);

        } else {
            holder = (ViewHolder) convertView.getTag();
        }
        //加载数据
        Diary diary = (Diary) itemList.get(position);
        holder.title_diary.setText(diary.getTitle());
        holder.content_diary.setText(diary.getContent());
        return convertView;
	}

	class ViewHolder {
        public ImageView cover_diary;
        public TextView title_diary;
        public TextView content_diary;
    }
}
